package dk.muldbjergskolen.gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;

import dk.muldbjergskolen.ctr.InventarCtr;
import dk.muldbjergskolen.model.Inventar;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

public class RetInventar extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField textField_Navn;
	private JTextField textField_Beskrivelse;
	private JTextField textField_Antal;
	private InventarCtr iC = new InventarCtr();
	private JFrame frame;
	private JComboBox comboBox_Inventar;


	/**
	 * Create the dialog.
	 */
	public RetInventar(RetInventarListener listener) {
		retInventarListener = listener;
		
		setModalityType(ModalityType.APPLICATION_MODAL);
		setTitle("Ret Inventar");
		setBounds(100, 100, 278, 244);
		getContentPane().setLayout(new BorderLayout());
		JPanel buttonPane = new JPanel();
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			JLabel lblNavn = new JLabel("Navn:");
			lblNavn.setBounds(10, 58, 57, 14);
			contentPanel.add(lblNavn);
		}
		{
			JLabel lblAntal = new JLabel("Antal:");
			lblAntal.setBounds(10, 86, 57, 14);
			contentPanel.add(lblAntal);
		}
		{
			JLabel lblBeskrivelse = new JLabel("Beskrivelse:");
			lblBeskrivelse.setBounds(10, 114, 57, 14);
			contentPanel.add(lblBeskrivelse);
		}
		
		textField_Navn = new JTextField();
		textField_Navn.setBounds(102, 55, 132, 20);
		contentPanel.add(textField_Navn);
		textField_Navn.setColumns(10);
		
		textField_Antal = new JTextField();
		textField_Antal.setBounds(102, 83, 132, 20);
		contentPanel.add(textField_Antal);
		textField_Antal.setColumns(10);
		
		textField_Beskrivelse = new JTextField();
		textField_Beskrivelse.setBounds(102, 111, 132, 20);
		contentPanel.add(textField_Beskrivelse);
		textField_Beskrivelse.setColumns(10);
		
		JLabel vælg_fag = new JLabel("Vælg Inventar:");
		vælg_fag.setBounds(10, 23, 86, 14);
		contentPanel.add(vælg_fag);
		
		comboBox_Inventar = new JComboBox();
		comboBox_Inventar.setModel(new DefaultComboBoxModel(new String[] {"fjernsyn"}));
		comboBox_Inventar.setBounds(102, 20, 132, 20);
		contentPanel.add(comboBox_Inventar);
		opdaterInventar();
		
		buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						ret();
						
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						setVisible(false);
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	
	
	
	//Opdatere ComboBoxen klasser, så den indeholder de klasser der er i databasen.
	public void opdaterInventar()
	{
		try
		{
			comboBox_Inventar.removeAllItems();
			ArrayList<dk.muldbjergskolen.model.Inventar> a = iC.findInventar();
		
			for(int i = 0; i < a.size(); i++)
			{
				comboBox_Inventar.addItem(a.get(i).getId());
			}
		}
		
		catch(Exception e)
		{
			JOptionPane.showMessageDialog(this, "Ingen Inventar blev fundet.");
		}
	}

	//
	// Event håndtering.
	//

	// "Lyttere"
	RetInventarListener retInventarListener;

	public interface RetInventarListener {
		void OnRettetInventar();
	}

	/***
	 * Kaldes når en ny Lokale er lavet.
	 */
	private void OnRettetInventar() {
		if (retInventarListener != null)
			retInventarListener.OnRettetInventar();
	}
	
	//retter et inventar
	public void ret()
	{
		try
		{
			int glId = Integer.parseInt(comboBox_Inventar.getSelectedItem().toString());
			Inventar i = new Inventar(-1, textField_Navn.getText(), textField_Beskrivelse.getText(), Integer.parseInt(textField_Antal.getText()));
			iC.retInventar(i, glId);
		}
		catch (Exception e)
		{
			JOptionPane.showMessageDialog(frame, "Udfyld alle Felterne");
			return;
		}
		OnRettetInventar();
		setVisible(false);
	}
}
